package saves.objects;

import game.Configurations;
import images.ImageInfo;
import images.ObjectLoader;
import java.awt.Color;
import math.Point;

public class SeletorBloco extends Corpo {

    private static final long serialVersionUID = -6688913911846033327L;
    private Point pontoInicial, pontoFinal;
    private boolean visivel;
    
    private SeletorBloco() {}

    public SeletorBloco(float x, float y, Color cor) {
        this(x, y, Configurations.BLOCK_SIZE, Configurations.BLOCK_SIZE, cor);
    }

    public SeletorBloco(float x, float y, Color cor, boolean visivel) {
        this(x, y, Configurations.BLOCK_SIZE, Configurations.BLOCK_SIZE, cor, visivel);
    }

    public SeletorBloco(float x, float y, int l, int a, Color cor) {
        this(x, y, l, a, cor, true);
    }

    public SeletorBloco(float x, float y, int l, int a, Color cor, boolean visivel) {
        super(x, y, getImagem(cor), null);
        this.pontoInicial = new Point(x, y);
        this.pontoFinal = new Point(x, y);
        this.visivel = visivel;
    }

    public void mover(float dx, float dy) {
        this.x += Configurations.BLOCK_SIZE * dx;
        this.y += Configurations.BLOCK_SIZE * dy;
    }

    public void setPontoFinal(Point p) {
        this.pontoFinal = p;
        if (p.x < pontoInicial.x) {
            float aux = x;
            x = p.x;
            p.x = aux;
        }
        if (p.y < pontoInicial.y) {
            float aux = y;
            y = p.y;
            p.y = aux;
        }
    }
    
    public static int getImagem(Color c) {
        if (c.equals(Color.BLUE))
            return 6;
        if (c.equals(Color.RED))
            return 7;
        return 8;
    }
    
    public static void draw(int x, int y, int w, int h, Color c) {
        ImageInfo.drawColorSquare(x, y, w, h, c, false);
    }

    public void setVisivel(boolean visivel) {
        this.visivel = visivel;
    }

    public boolean isVisivel() {
        return this.visivel;
    }

    public Point getPontoInicial() {
        return pontoInicial;
    }

    public Point getPontoFinal() {
        return pontoFinal;
    }

    @Override
    public void draw(int x, int y) {
        draw(x, y, Configurations.BLOCK_SIZE);
    }
    
    public void draw(int x, int y, int s) {
        if (this.isVisivel()) {
            int w = s * Math.max((int) Math.abs(pontoFinal.x - pontoInicial.x), Configurations.BLOCK_SIZE) / Configurations.BLOCK_SIZE,
                h = s * Math.max((int) Math.abs(pontoFinal.y - pontoInicial.y), Configurations.BLOCK_SIZE) / Configurations.BLOCK_SIZE;
            ObjectLoader.drawObject(codImagem, x + w/2, y + h/2, w, h);
        }
    }
}
